import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.util.*;


public class Board extends JPanel
{
	private JPanel buttonPanel;
	private Spot spot[][];
	private ButtonListener listener;
	private final int SIZE = 8;
	private final int IMAGE_SIZE = 60;
	private ArrayList<Location> list;
	private Location source;
	
	ImageIcon emptySpot;
	
	public Board()
	{
		createVariables();
		
	}
	
	public void createVariables()
	{
		source = new Location();
		list = new ArrayList<Location>();
		spot = new Spot[SIZE][SIZE];
//		emptySpot = new ImageIcon("images/emptySpot.gif", "an empty space");
		listener = new ButtonListener();
		buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(IMAGE_SIZE*SIZE, IMAGE_SIZE*SIZE));
		buttonPanel.setBackground(Color.blue);
		buttonPanel.setLayout(new GridLayout(SIZE, SIZE, 0, 0));
		
		for(int x = 0; x < SIZE; x++)
		{
			for(int y = 0; y < SIZE; y++)
			{
				spot[x][y] = new Spot(x, y, true, listener);
				buttonPanel.add(spot[x][y].getButton());
			}
		}
		
		setupPieces();
		
		setPreferredSize(new Dimension(875, 750));
		setBackground(Color.cyan);
		add(buttonPanel);
	}
	
	public void setupPieces()
	{
		//Black Pieces
		spot[0][0].setPiece(new Rook(0, 0, false));
		spot[0][1].setPiece(new Knight(0, 1, false));
		spot[0][2].setPiece(new Bishop(0, 2, false));
		spot[0][3].setPiece(new Queen(0, 3, false));
		spot[0][4].setPiece(new King(0, 4, false));
		spot[0][5].setPiece(new Bishop(0, 5, false));
		spot[0][6].setPiece(new Knight(0, 6, false));
		spot[0][7].setPiece(new Rook(0, 7, false));
		
		spot[1][0].setPiece(new Pawn(1, 0, false));
		spot[1][1].setPiece(new Pawn(1, 1, false));
		spot[1][2].setPiece(new Pawn(1, 2, false));
		spot[1][3].setPiece(new Pawn(1, 3, false));
		spot[1][4].setPiece(new Pawn(1, 4, false));
		spot[1][5].setPiece(new Pawn(1, 5, false));
		spot[1][6].setPiece(new Pawn(1, 6, false));
		spot[1][7].setPiece(new Pawn(1, 7, false));
		
		//White Pieces
		spot[6][0].setPiece(new Pawn(6, 0, true));
		spot[6][1].setPiece(new Pawn(6, 1, true));
		spot[6][2].setPiece(new Pawn(6, 2, true));
		spot[6][3].setPiece(new Pawn(6, 3, true));
		spot[6][4].setPiece(new Pawn(6, 4, true));
		spot[6][5].setPiece(new Pawn(6, 5, true));
		spot[6][6].setPiece(new Pawn(6, 6, true));
		spot[6][7].setPiece(new Pawn(6, 7, true));
		
		spot[7][0].setPiece(new Rook(7, 0, true));
		spot[7][1].setPiece(new Knight(7, 1, true));
		spot[7][2].setPiece(new Bishop(7, 2, true));
		spot[7][3].setPiece(new Queen(7, 3, true));
		spot[7][4].setPiece(new King(7, 4, true));
		spot[7][5].setPiece(new Bishop(7, 5, true));
		spot[7][6].setPiece(new Knight(7, 6, true));
		spot[7][7].setPiece(new Rook(7, 7, true));
		
		
		
	}
	
	public void deHighlightAll()
	{
		for(int k = 0; k <list.size(); k++)
		{
			spot[list.get(k).getX()][list.get(k).getY()].getPiece().deHighlight();
			spot[list.get(k).getX()][list.get(k).getY()].update();
		}
	}
	
	
	
	
	class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			try
			{
				for(int x = 0; x < SIZE; x++)
				{
					for(int y = 0; y < SIZE; y++)
					{
						if(event.getSource() == spot[x][y].getButton())
						{
							System.out.println(spot[x][y].getPiece().isHighlightedRed());
							if(spot[x][y].getPiece().isHighlightedRed())
							{
								spot[source.getX()][source.getY()].getPiece().moveTo(x, y);
								System.out.println("x=" + source.getX() + ",y=" + source.getY());
								spot[x][y].setPiece(spot[source.getX()][source.getY()].getPiece());
								spot[x][y].update();
								spot[source.getX()][source.getY()].setPiece(new Empty(source.getX(), source.getY()));
								deHighlightAll();
							}
							else
							{
								source.setLoc(x, y);
								System.out.println("Set:x=" + source.getX() + ",y=" + source.getY());
								deHighlightAll();
								list = spot[x][y].getPiece().getPossibleMoves(spot);
								
								
// Checks if Colors match & removes from possibilities list if they do
								/*
								for(int k = 0; k < list.size(); k++)
								{
									if(spot[x][y].getPiece().isSameColorAs(spot[list.get(k).getX()][list.get(k).getY()].getPiece()))
									{
										list.remove(k);
										k--;
									}
								}
								*/
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
								
								
								
								
								for(int k = 0; k < list.size(); k++)
								{
									System.out.println("LIGHTING:x=" + list.get(k).getX() + ",y=" + list.get(k).getY());
									spot[list.get(k).getX()][list.get(k).getY()].getPiece().highlightRed();
									spot[list.get(k).getX()][list.get(k).getY()].update();
								}
							
							}
						}
					}
				}
			}catch(Exception e){}
		}
	}
}
